package cn.wolfcode.crm.web.Controller;

import cn.wolfcode.crm.Service.IPermissionService;
import cn.wolfcode.crm.Service.IRoleService;
import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.mapper.RoleMapper;
import cn.wolfcode.crm.query.RoleQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;
    @Autowired
    private IPermissionService permissionService;
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") RoleQuery qo) {

        PageInfo<Role> pageResult = roleService.queryForList(qo);
        model.addAttribute("pageResult", pageResult);
        return "role/list";//跳转到list.jsp页面/WEB-INF/view/role/list.flt
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role ,Long[] ids) {
        System.out.println(role.getId());
        if (role.getId() == null) {
            roleService.insert(role);
            //返回可用主键
            if (ids!=null && ids.length !=0 ){
                for (Long id : ids) {
                    roleService.insertRelation(role.getId(),id);
                }
            }
        }else {
            //需要将关系数据保存到中间表 role-permission
            if (ids!=null && ids.length !=0 ){
                for (Long id : ids) {
                    roleService.insertRelation(role.getId(),id);
                }
            }
            roleService.updateByPrimaryKey(role);
        }

        return "redirect:/role/list";//跳转到list.jsp页面/WEB-INF/view/role/list.flt
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            if (id != null){
                //删除角色关系数据表
                roleService.deleteRelation(id);
                //删除角色 role
                roleService.deleteByPrimaryKey(id);
            }
        }catch (Exception e){
            e.printStackTrace();
            result.mark("删除失败");
        }
        return result;
    }
    @RequestMapping("/input")
    public String input(Model model,Long id) {
        //得到所有的权限
        List<Permission> permissions = permissionService.selectAll();
        model.addAttribute("permission",permissions);
        if (id!=null){
            //得到当前对象的权限
            List<Permission> presentPermission = roleService.getPresentPermission(id);
            model.addAttribute("presentPermission",presentPermission);
            Role role = roleService.selectOne(id);
            model.addAttribute("role",role);
            System.out.println(role);
        }
        return "role/input";//跳转到list.jsp页面/WEB-INF/view/role/list.flt
    }
}
